package pattern.config.datasource;/**
 * @program pattern
 * @author Simon
 * @date 2020-10-10 16:32
 **/

import com.zaxxer.hikari.HikariDataSource;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
 * 初始化数据源
 * @author Simon
 */
@Configuration
public class PatternMainDataSource {

    @Bean("dataSourceOne")
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource dataSourceOne(DataSourceProperties properties) {
        HikariDataSource hikariDataSource = DataSourceBuilder.create(properties.getClassLoader())
                .type(HikariDataSource.class)
                .driverClassName(properties.determineDriverClassName())
                .url(properties.determineUrl())
                .username(properties.determineUsername())
                .password(properties.determinePassword())
                .build();
        return hikariDataSource;
    }
    @Bean("dataSourceTwo")
    @ConfigurationProperties(prefix = "spring.datasource.two")
    public DataSource dataSourceTwo(DataSourceProperties properties) {
        HikariDataSource hikariDataSource = DataSourceBuilder.create(properties.getClassLoader())
                .type(HikariDataSource.class)
                .driverClassName(properties.determineDriverClassName())
                .url(properties.determineUrl())
                .username(properties.determineUsername())
                .password(properties.determinePassword())
                .build();
        return hikariDataSource;
    }

}
